﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.LoaiGiayToService;
namespace eTMS.Controllers{
public class LoaiGiayToControllerBase : CommonController{
	public LoaiGiayToControllerBase()
		: base()
	{	
		this.dao = new LoaiGiayToServiceClient();
	}
	~LoaiGiayToControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private LoaiGiayToEntity oLoaiGiayTo;	
	public LoaiGiayToEntity LoaiGiayToObject{
		get{
			return oLoaiGiayTo;
		}
		set{
			oLoaiGiayTo=value;
		}
	}
	protected LoaiGiayToServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSLoaiGiayTo(false);			
			return _Table;
		}
	}
	public void LoadAllDSLoaiGiayTo(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oLoaiGiayTo==null)return false;
		bool result=true;
		try{
			oLoaiGiayTo = dao.SaveRet(oLoaiGiayTo);
		}
		catch{
			result=false;
		}
		if(result){
			_ListLoaiGiayTo.Add(oLoaiGiayTo);
		}
		return result;
	}
	public bool Update(){
		if(oLoaiGiayTo==null)return false;
		return dao.Update(oLoaiGiayTo);
	}
	public bool Delete(){
		if(oLoaiGiayTo==null)return false;
		bool retvalue = dao.Delete(oLoaiGiayTo);
		if(retvalue){			
			_ListLoaiGiayTo.Remove(oLoaiGiayTo);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 LoaiGiayTo_ID){
		bool retvalue = dao.DeleteByID(LoaiGiayTo_ID);		
		if(retvalue){
			LoaiGiayToEntity oDeleted=FindLoaiGiayTo(LoaiGiayTo_ID);
			if(oDeleted!=null){
				_ListLoaiGiayTo.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oLoaiGiayTo = new LoaiGiayToEntity();
	}
	//Check if exists MaLoaiGiayTo
	public bool CheckExistsMaLoaiGiayTo(object value){	
		return dao.SelectOneByMaLoaiGiayTo(value.ToString()).MaLoaiGiayTo!=null;
    }    
    //Copy data from data row in grid to object
    public LoaiGiayToEntity MapDataRowToObject(DataRow row){
		LoaiGiayToEntity o = new LoaiGiayToEntity();
		 o.MaLoaiGiayTo=Convert.ToString(row["MaLoaiGiayTo"]);
		 o.TenLoaiGiayTo=Convert.ToString(row["TenLoaiGiayTo"]);
		 o.MoTaLoaiGiayTo=Convert.ToString(row["MoTaLoaiGiayTo"]);
		 if(!row.IsNull("LoaiGiayTo_ID"))
		    o.LoaiGiayTo_ID=(Int32)(row["LoaiGiayTo_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(LoaiGiayToEntity o,DataRow row){
		row["MaLoaiGiayTo"]=o.MaLoaiGiayTo;
		row["TenLoaiGiayTo"]=o.TenLoaiGiayTo;
		row["MoTaLoaiGiayTo"]=o.MoTaLoaiGiayTo;
		row["LoaiGiayTo_ID"]=o.LoaiGiayTo_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.LoaiGiayToObject,row);
    }
	public void LoadObject(Int32 LoaiGiayTo_ID){
		LoaiGiayToEntity oLoad=FindLoaiGiayTo(LoaiGiayTo_ID);
		if(oLoad==null){
			oLoaiGiayTo=this.LoadLoaiGiayTo(LoaiGiayTo_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oLoaiGiayTo.IsNullLoaiGiayTo_ID;
			if(!bNotIsNull){
				this.ListLoaiGiayTo.Add(oLoaiGiayTo);
			}
		}
		else{
			oLoaiGiayTo=oLoad;
		}
	}
	#region CachedList LoaiGiayTo
	private static List<LoaiGiayToEntity> _ListLoaiGiayTo=new List<LoaiGiayToEntity>();
    public List<LoaiGiayToEntity>ListLoaiGiayTo{
		get{
			return _ListLoaiGiayTo;
		}
	}
	public static List<LoaiGiayToEntity>ListLoaiGiayToStatic{
		get{
			return _ListLoaiGiayTo;
		}
	}
	public LoaiGiayToEntity FindLoaiGiayTo(Int32 LoaiGiayTo_ID){
		LoaiGiayToEntity oResult=this.ListLoaiGiayTo.Find(delegate(LoaiGiayToEntity oFind){
			bool result=true; 
			result&=oFind.LoaiGiayTo_ID.Equals(LoaiGiayTo_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllLoaiGiayTo(bool clearCache){
		if(_ListLoaiGiayTo.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		LoaiGiayToServiceClient dao = new LoaiGiayToServiceClient();
		_ListLoaiGiayTo=new List<LoaiGiayToEntity>(dao.SelectAll());
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	public LoaiGiayToEntity LoadLoaiGiayTo(Int32 LoaiGiayTo_ID){			
		LoaiGiayToEntity o=dao.SelectOne(LoaiGiayTo_ID);		
		return o;
	}
	#endregion
}
}
